//
//  XJCommunityNearView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/5.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  附近社区view

#import "XJCommunityNearView.h"

//Model
#import "XJCommunityModel.h"

#import "XJUserModel.h"

@interface XJCommunityNearView()
@property(nonatomic, strong) UILabel *lastLabel;

@end

@implementation XJCommunityNearView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

#pragma mark - 一 重写setter方法
// 宽度定死:(屏幕宽度 - 间距）/4
- (void)setNearArray:(NSMutableArray *)nearArray {
    
    _nearArray = nearArray;
    if (_nearArray.count == 0) {
        self.hidden = YES;
        return;
    }
    
    self.hidden = NO;
    
    CGFloat margin = 10;
    CGFloat w = (KWidth - 10*8)/4;
    CGFloat kTagTitleHeight = 20.0f; // 标题高度
    
    // 附近小区
    UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, 10, 100, kTagTitleHeight)];
    [self addSubview:tipsLabel];
    
    tipsLabel.text = @"附近社区";
    for (int i=0; i<_nearArray.count; i++) {
        UILabel *lb = [UILabel new];
        lb.layer.masksToBounds = YES;
        lb.layer.cornerRadius = 5;
        lb.layer.borderColor = [UIColor blackColor].CGColor;
        lb.layer.borderWidth = 1.0;
        XJCommunityModel *model = nearArray[i];
        lb.text = model.name;
        lb.tag = i;
        
        CGFloat x = margin;
        if (i>0) {
            x = margin + (2*margin + w) * i;
        }
        
        lb.frame = CGRectMake(x, tipsLabel.rb_bottom + 10, w, kTagTitleHeight);
        [self addSubview:lb];
        
        
        lb.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLbGesgure:)];
        [lb addGestureRecognizer:tapGes];
    }
}


//这个是动态计算宽度的
//- (void)setNearArray:(NSMutableArray *)nearArray {
//    _nearArray = nearArray;
//
//    if (_nearArray.count == 0) {
//        self.hidden = YES;
//        return;
//    }
//
//    self.hidden = NO;
//
//
//    _lastLabel = nil; // 默认nil
//
//
//    CGFloat s_w = self.frame.size.width;
//    CGFloat kTagTitleHeight = 20.0f; // 标题高度
//
//    CGFloat kTitleHorizontal_space =  10.0f; // 字体离边框的水平距离
//    CGFloat kTitleVertical_space = 5.0f; // 字体边框的垂直距离
//    CGFloat kTagScreen_margin = 12.f; /** tagLab与屏幕左右间距 */
//
//    CGFloat kTagHorizontal_margin = 15.0f; /** tagLab之间的水平间距 */
//    CGFloat kTagVertical_margin = 10.0f; /** tagLab之间的竖直间距 */
//
//
//    // 附近小区
//    UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, s_w, kTagTitleHeight)];
//    [self addSubview:tipsLabel];
//    tipsLabel.text = @"    附近社区";
//
//
//    for (int i =0; i<_nearArray.count; i++) {
//        UILabel *tagLabel = [UILabel new];
////        NSString *textStr = _nearArray[i];
//
//        XJCommunityModel *model = _nearArray[i];
//        NSString *textStr = model.name;
//
//        tagLabel.text = textStr;
//        UIFont *kTagFont =  [UIFont boldSystemFontOfSize:15];
//
//        CGSize tagSize = [textStr sizeWithAttributes:@{NSFontAttributeName:kTagFont}];
//        CGFloat w = tagSize.width + kTitleHorizontal_space *2;
//        CGFloat h = tagSize.height + kTitleVertical_space *2;
//
//
//        XJLog(@"算出来的宽度不对:%f",w);
//
//        if (0 == i) { // 先固定第一位置
//            tagLabel.frame = CGRectMake(kTagScreen_margin, tipsLabel.rb_bottom + kTitleVertical_space*2, w, h);
//        }else {
//
//            CGFloat maxRight = _lastLabel.rb_right + kTagHorizontal_margin + w;
//
//            if (maxRight > s_w) {//换行
//                tagLabel.frame = CGRectMake(kTagScreen_margin, _lastLabel.rb_bottom + kTagVertical_margin, w, h);
//            }else { // 不换行
//                tagLabel.frame = CGRectMake(_lastLabel.rb_right + kTagHorizontal_margin, _lastLabel.rb_y, w, h);
//            }
//        }
//
//
//        _lastLabel = tagLabel;
//        [self addSubview:tagLabel];
//
//
//        // 配置tagLabel
//        [self configLabel:tagLabel];
//
//        // 计算self 的高度
//        if (i == _nearArray.count - 1) {
//            CGRect tempFrame = self.frame;
//            tempFrame.size.height = _lastLabel.rb_bottom + kTitleVertical_space;
//            self.frame = tempFrame;
//        }
//    }
//
//}

//- (void)configLabel:(UILabel *)tag {
//
//
//    tag.layer.borderWidth = 1.0;
//    tag.layer.borderColor = [UIColor blackColor].CGColor;
//    tag.layer.cornerRadius = 3;
//    tag.layer.masksToBounds = YES;
//    tag.userInteractionEnabled = YES;
//
//    tag.textAlignment = NSTextAlignmentCenter;
//
//    //给文本添加点击
//    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSubTagView:)];
//    tapOne.numberOfTapsRequired = 1.0;
//    [tag addGestureRecognizer:tapOne];
//}

#pragma mark -- 点击文本
//- (void)touchSubTagView:(UITapGestureRecognizer*)tapOne
//{
//    UILabel *tag = (UILabel *)tapOne.view;
//    XJLog(@"==========%@",tag.text);
//
//    if (self.clickTagBlock) {
//        self.clickTagBlock(tag.text);
//    }
//}


- (void)clickLbGesgure:(UITapGestureRecognizer *)tap {
    UILabel *lb =    (UILabel *)tap.view;
    XJLog(@"%@ - %ld",lb.text ,lb.tag);
    
    XJCommunityModel *model = self.nearArray[lb.tag];
    if (self.clickTagBlock) {
        //用户id
        self.clickTagBlock(model);
    }
}



@end
